#include <hacks/base/spin_lock.h>

namespace hacks {

    spin_lock::spin_lock() :
            is_lock_(false) {
        assert(is_lock_.is_lock_free());
    }

    bool spin_lock::try_lock() {
        if (is_lock_.load()) {
            return false;
        }
        lock();
        return true;
    }

    void spin_lock::lock() {
        auto expect = false;
        while (!is_lock_.compare_exchange_strong(expect, true, std::memory_order_acq_rel));
    }

    void spin_lock::unlock() {
        assert(is_lock_);
        is_lock_.store(false, std::memory_order_release);
    }
}
